
#include"boxgame.h"
#include<QString>
#include<QImage>
#include<QRect>
#include<memory.h>
#include<QPainter>


BoxGame::BoxGame() {
    initMap();
}
void BoxGame::initMap() {
    int graph[8][8] = {
        {1,1,1,1,1,1,1,1},
        {1,3,4,0,0,0,3,1},
        {1,0,1,1,1,0,4,0},
        {1,0,0,5,0,0,0,0},
        {1,0,1,0,1,0,1,1},
        {1,0,1,0,1,0,1,1},
        {1,0,0,0,4,3,0,1},
        {1,1,1,1,1,1,1,1}
    };
    step=0;
    memcpy(&map[0][0], &graph[0][0], sizeof(int) * 64);
}
void BoxGame::bushuqi(QPainter*painter){
    QFont font("方正舒体",30,QFont::ExtraLight,false);
                                   QPen pen;
               pen.setColor(Qt::white);
               painter->setPen(pen);
               painter->setFont(font);
               painter->drawText(40,40,QString("剩余步数：%1").arg(MAX_STEP-step));
if(gameover()){
                 painter->drawText(40,480,QString("任务成功！坤坤谢谢你")) ;
               }

if(step>MAX_STEP){
                painter->drawText(40,480,QString("任务失败！坤坤对你很失望")) ;
               }
}
void BoxGame::drawMap(QPainter* pDraw) {

    QString imgURL;
    for (int i = 0;i < 8;i++) {
        for (int j = 0;j < 8;j++) {

            switch (map[i][j]) {
            case 0:
                imgURL = "://tupian/road.jpg";
                break;
            case 1:
                imgURL = "://tupian/stone.png";
                break;
            case 3:
                imgURL = "://tupian/lanwang.jpg";
                break;
            case 4:
                imgURL = "://tupian/basketball.jpg";
                break;
            case 5:
            case 8:
                imgURL = "://tupian/kun.jpg";
                break;
            case 7:
                imgURL = "://tupian/chicken.jpg";
                break;

            }
            QImage img(imgURL);
            pDraw->drawImage(QRect(64*j,64*i,64,64),img);

        }
    }
}
int BoxGame::getRow() {
    for (int i = 0;i < 8;i++) {
        for (int j = 0;j < 8;j++) {
            if (map[i][j] == 5 || map[i][j] == 8) {
                return i;
            }
        }
    }
    return -1;
}
int BoxGame::getCols() {
    for (int i = 0;i < 8;i++) {
        for (int j = 0;j < 8;j++) {
            if (map[i][j] == 5 || map[i][j] == 8) {
                return j;
            }
        }
    }
    return -1;
}
void BoxGame::keyDown(QKeyEvent* event) {
    int i = getRow();
    int j = getCols();
    switch (event->key()) {
    case Qt::Key_W:
    case Qt::Key_Up:
        if (map[i - 1][j] == 0 || map[i - 1][j] == 3) {
            map[i][j] -= 5;
            map[i - 1][j] += 5;
            step++;
        }
        if (map[i - 1][j] == 4 || map[i - 1][j] == 7) {
            if (map[i - 2][j] == 0 || map[i - 2][j] == 3) {
                map[i][j] -= 5;
                map[i - 1][j] += 1;
                map[i - 2][j] += 4;
                step++;
            }
        }
        break;
    case Qt::Key_S:
    case Qt::Key_Down:
        if (map[i + 1][j] == 0 || map[i + 1][j] == 3) {
            map[i][j] -= 5;
            map[i + 1][j] += 5;
            step++;
        }
        if (map[i + 1][j] == 4 || map[i + 1][j] == 7) {
            if (map[i + 2][j] == 0 || map[i + 2][j] == 3) {
                map[i][j] -= 5;
                map[i + 1][j] += 1;
                map[i + 2][j] += 4;
                step++;
            }
        }
        break;
    case Qt::Key_A:
    case Qt::Key_Left:
        if (map[i][j - 1] == 0 || map[i][j - 1] == 3) {
            map[i][j] -= 5;
            map[i][j - 1] += 5;
            step++;
        }
        if (map[i][j - 1] == 4 || map[i][j - 1] == 7) {
            if (map[i][j - 2] == 0 || map[i][j - 2] == 3) {
                map[i][j] -= 5;
                map[i][j - 1] += 1;
                map[i][j - 2] += 4;
                step++;
            }
        }
        break;
    case Qt::Key_D:
    case Qt::Key_Right:
        if (map[i][j + 1] == 0 || map[i][j + 1] == 3) {
            map[i][j] -= 5;
            map[i][j + 1] += 5;
            step++;
        }
        if (map[i][j + 1] == 4 || map[i][j + 1] == 7) {
            if (map[i][j + 2] == 0 || map[i][j + 2] == 3) {
                map[i][j] -= 5;
                map[i][j + 1] += 1;
                map[i][j + 2] += 4;
                step++;
            }
        }
        break;
    }
}
bool BoxGame::gameover() {
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            if(map[i][j]==4){
                return false;
            }
        }
    }
    return true;
}


